package com.cskaoyan.service.admin.impl;

import com.cskaoyan.bean.BaseParam;
import com.cskaoyan.bean.BaseRespData;
import com.cskaoyan.bean.pojo.Comment;
import com.cskaoyan.bean.pojo.CommentExample;
import com.cskaoyan.mapper.CommentMapper;
import com.cskaoyan.service.admin.CommentService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Classname CommentServiceImpl
 * @Description
 * @Date 2021/8/12 19:54
 * @Author : xujun
 */
@Service
@Transactional
public class CommentServiceImpl implements CommentService {

    @Autowired
    CommentMapper commentMapper;

    @Override
    public void commentDelete(Comment comment) {
        int i = commentMapper.updateByPrimaryKey(comment);
    }

    @Override
    public BaseRespData commentList(Integer valueId, Integer userId, BaseParam bo) {

        PageHelper.startPage(bo.getPage(), bo.getLimit());

        CommentExample example = new CommentExample();
        example.setOrderByClause(bo.getSort() + " " + bo.getOrder());

        CommentExample.Criteria criteria = example.createCriteria();
        // 因为是逻辑删除，所以已删除的不能再显示
        criteria.andDeletedEqualTo(false);
        if (userId != null) {
            criteria.andUserIdEqualTo(userId);
        }
        if (valueId != null) {
            criteria.andValueIdEqualTo(valueId);
        }

        List<Comment> comments = commentMapper.selectByExample(example);

        PageInfo<Comment> pageInfo = new PageInfo<>(comments);
        long total = pageInfo.getTotal();

        return BaseRespData.create(comments, total);

    }
}
